﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XCommunity.Views;
using System.Xml.Linq;

namespace XCommunity.Forums.Views
{


  /// <summary>
  /// 定义表现为论坛架构容器的视图的基类型
  /// </summary>
  public class PanelFrameView : ViewObject
  {

    public PanelFrameView( ForumsViewService service, XElement configElement )
      : base( service )
    {

      ConfigElement = configElement;

    }



    /// <summary>
    /// 节点的配置节
    /// </summary>
    protected XElement ConfigElement
    {
      get;
      private set;
    }

    public virtual string Name
    {
      get { return ConfigElement.Attribute( "name" ).Value; }
    }

    public virtual string Route
    {
      get { return GetRoute( ConfigElement ); }
    }


    protected string GetRoute( XElement node )
    {
      if ( node.Name == ForumsSystem.XF + "forums" )
        return node.Attribute( "name" ).Value;
      else
        return GetRoute( node.Parent ) + "/" + node.Attribute( "name" ).Value;
    }




    public string Title
    {
      get { return ConfigElement.Element( ForumsSystem.XF + "title" ).Value; }
    }


    public string Description
    {
      get
      {
        var descriptionElement = ConfigElement.Element( ForumsSystem.XF + "description" );

        if ( descriptionElement == null )
          return null;
        else
          return descriptionElement.Value;
      }
    }




    protected PanelFrameView[] GetSubset()
    {

      return
        ( from element in ConfigElement.Elements()
          where element.Name == ForumsSystem.XF + "panel"
          select (PanelFrameView) ( (ForumsViewService) Service ).GetView( Route + "/" + element.Attribute( "name" ).Value )
        ).ToArray();

    }



    private PanelFrameView[] _childs;

    /// <summary>
    /// 子版面
    /// </summary>
    public PanelFrameView[] ChildPanels
    {
      get
      {
        if ( _childs == null )
          _childs = GetSubset();

        return _childs;
      }
    }


    public override object BaseObject
    {
      get { return null; }
    }


  }
}
